package com.citywy.view;

import com.citywy.entity.News;
import com.citywy.service.NewsService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(name = "NewsDetailController", value = "/news/*")
public class NewsDetailController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String regex="[^0-9]";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(uri);
        System.out.println(m.replaceAll("").trim()+"------执行几次---------");
        int id = Integer.parseInt(m.replaceAll("").trim());
        System.out.println("输出ID:"+id);
        NewsService newsService = new NewsService();
        News news = newsService.get(id);
        request.setAttribute("news",news);
        //上一篇 下一篇
        News tNews = newsService.get(id-1);
        News nNews = newsService.get(id+1);
        request.setAttribute("tnews",tNews);
        request.setAttribute("nnews",nNews);
        request.getRequestDispatcher("/pages/news_detail.jsp").forward(request,response);
    }
}
